//
//  NSObject+MDHHook.m
//  JSTech
//
//  Created by 高得华 on 2019/9/20.
//  Copyright © 2020 华&梅. All rights reserved.
//

#import "NSObject+MDHHook.h"
#import <objc/runtime.h>
#import "MDHFileConstant.h"

static NSString *_errorFunctionName;
void dynamicMethodIMP(id self,SEL _cmd) {
#ifdef DEBUG
    NSString *error = [NSString stringWithFormat:@"errorClass->:%@\n errorFuction->%@\n errorReason->UnRecognized Selector",NSStringFromClass([self class]),_errorFunctionName];
    AlertViewShowOneTB(@"程序异常", error, ^(int index) {
        
    });
#else
    //upload error
    
#endif
}

#pragma mark 方法调换
static inline void change_method(Class _originalClass ,SEL _originalSel,Class _newClass ,SEL _newSel){
    Method methodOriginal = class_getInstanceMethod(_originalClass, _originalSel);
    Method methodNew = class_getInstanceMethod(_newClass, _newSel);
    method_exchangeImplementations(methodOriginal, methodNew);
}


@implementation NSObject (MDHHook)

+ (void)load{
    change_method([self class], @selector(methodSignatureForSelector:), [self class], @selector(SH_methodSignatureForSelector:));
    change_method([self class], @selector(forwardInvocation:), [self class], @selector(SH_forwardInvocation:));
}

- (NSMethodSignature *)SH_methodSignatureForSelector:(SEL)aSelector{
    /*if ([self isKindOfClass:[SSDKUser class]] || [self isKindOfClass:[MOBFDataModel class]]) {
        return [self SH_methodSignatureForSelector:aSelector];
    }*/
    if (![self respondsToSelector:aSelector]) {
        _errorFunctionName = NSStringFromSelector(aSelector);
        NSMethodSignature *methodSignature = [self SH_methodSignatureForSelector:aSelector];
        if (class_addMethod([self class], aSelector, (IMP)dynamicMethodIMP, "v@:")) {//方法参数的获取存在问题
            NSLog(@"临时方法添加成功！");
        }
        if (!methodSignature) {
            methodSignature = [self SH_methodSignatureForSelector:aSelector];
        }
        
        return methodSignature;
        
    }else{
        return [self SH_methodSignatureForSelector:aSelector];
    }
}

- (void)SH_forwardInvocation:(NSInvocation *)anInvocation{
    SEL selector = [anInvocation selector];
    if ([self respondsToSelector:selector]) {
        [anInvocation invokeWithTarget:self];
    }else{
        [self SH_forwardInvocation:anInvocation];
    }
}

@end


@implementation UIViewController (MDHHook)

+ (void)load {
    Method pvacOld = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
    Method pvacNew = class_getInstanceMethod(self, @selector(mdh_presentViewController:animated:completion:));
    method_exchangeImplementations(pvacOld, pvacNew);
}

///进行 适配iOS 13.0新版本的 present的问题
- (void)mdh_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    if (@available(iOS 13.0, *)) {
        if (viewControllerToPresent.modalPresentationStyle == UIModalPresentationAutomatic) {
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverFullScreen;
        }
    }
    [self mdh_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end


@implementation NSURL (MDHHook)

+ (void)load {
    Method URLWithStringOld = class_getClassMethod(self, @selector(URLWithString:));
    Method URLWithStringNew = class_getClassMethod(self, @selector(mdh_URLWithString:));
    ///进行交换
    method_exchangeImplementations(URLWithStringOld, URLWithStringNew);
}

///检测URL中是否含有中文 则进行回避
+ (instancetype)mdh_URLWithString:(NSString *)URLString {
    if (URLString.length <= 0) {
        URLString = @"";
    }
    NSURL * url = [NSURL mdh_URLWithString:URLString];
    if (url == nil) {
    #ifdef DEBUG//测试环境
        NSString * mes = [NSString stringWithFormat:@"出现中文了:%@",URLString];
        DTLog(@"%@",mes);
        AlertViewShowOne(mes);
    #else
    #endif
        url = [NSURL mdh_URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    return url;
}

@end
